package com.xindaoapp.happypet.utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.util.Log;


public class HttpClientUtil {
    private final HttpClient client;

    private HttpPost post;
    private HttpGet get;
    private HttpResponse response;

    private final String TAG = "HttpClientUtil";

    private static Header[] headers;
    static {
        headers = new BasicHeader[10];
        headers[0] = new BasicHeader("Appkey", "12343");
        headers[1] = new BasicHeader("Udid", "");// �ֻ��
        headers[2] = new BasicHeader("Os", "android");//
        headers[3] = new BasicHeader("Osversion", "");//
        headers[4] = new BasicHeader("Appversion", "");// 1.0
        headers[5] = new BasicHeader("Sourceid", "");//
        headers[6] = new BasicHeader("Ver", "");
        headers[7] = new BasicHeader("Userid", "");
        headers[8] = new BasicHeader("Usersession", "");
        headers[9] = new BasicHeader("Unique", "");
    }

    public HttpClientUtil() {
        client = new DefaultHttpClient();
    }

    public String SendGet(String mapping, Map<String, String> params) {
        StringBuffer sbuf = new StringBuffer(Constants.URL + mapping);
        if (params != null) {
            Set<Entry<String, String>> entrySet = params.entrySet();
            sbuf.append("?");
            for (Entry<String, String> item : entrySet) {
                sbuf.append(item.getKey()).append("=").append(item.getValue()).append("&");
            }
        }
        get = new HttpGet(sbuf.toString());
        get.setHeaders(headers);
        try {
            response = client.execute(get);
            if (response.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(response.getEntity(), Constants.ENCONDING);
            } else {
                Log.i(TAG, "" + response.getStatusLine().getStatusCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String sendPost(String mapping, Map<String, String> params) {
        post = new HttpPost(Constants.URL + mapping);
        post.setHeaders(headers);
        // System.out.println(Constants.URL + mapping);
        // ���?ʱ
        HttpParams httpParams = new BasicHttpParams();//
        HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
        HttpConnectionParams.setSoTimeout(httpParams, 3000);
        post.setParams(httpParams);

        // ���ò���
        if (params != null && params.size() > 0) {
            List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
            for (Map.Entry<String, String> item : params.entrySet()) {
                BasicNameValuePair pair = new BasicNameValuePair(item.getKey(), item.getValue());
                parameters.add(pair);
            }
            try {
                UrlEncodedFormEntity entity =
                        new UrlEncodedFormEntity(parameters, Constants.ENCONDING);
                post.setEntity(entity);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        try {
            response = client.execute(post);
            if (response.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(response.getEntity(), Constants.ENCONDING);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
